package com.yanqu.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.data.entity.setting.CustomThresholdRatioData;
import com.yanqu.jdbc.JdbcTemplateHelper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CustomThresholdRatioDataDaoImpl extends TempDataObjectDao<CustomThresholdRatioData> {

    public long getMaxId() {
        String sql = "select ifnull(max(dbId),0) as maxId from t_s_monitor_goods_ratio_value ";
        long maxId = 0;
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(sql);
            if(sqlRowSet.next()) {
                maxId = sqlRowSet.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return maxId;
    }

    public static String getCustomThresholdRatioDataSql = "select * from `t_s_monitor_goods_ratio_value`";
    public Map<Integer, Map<Integer, Map<Integer, List<CustomThresholdRatioData>>>> getCustomThresholdRatioData() {
        Map<Integer, Map<Integer, Map<Integer, List<CustomThresholdRatioData>>>> resultMap = new ConcurrentHashMap<>();
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(getCustomThresholdRatioDataSql);
            while (sqlRowSet.next()) {
                CustomThresholdRatioData data = parse(sqlRowSet);
                if(!resultMap.containsKey(data.getChannelId())){
                    resultMap.put(data.getChannelId(), new ConcurrentHashMap<>());
                }
                if(!resultMap.get(data.getChannelId()).containsKey(data.getVip())){
                    resultMap.get(data.getChannelId()).put(data.getVip(), new ConcurrentHashMap<>());
                }
                if(!resultMap.get(data.getChannelId()).get(data.getVip()).containsKey(data.getThresholdType())){
                    resultMap.get(data.getChannelId()).get(data.getVip()).put(data.getThresholdType(), new ArrayList<>());
                }
                resultMap.get(data.getChannelId()).get(data.getVip()).get(data.getThresholdType()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }
        return resultMap;
    }

    public static String updateCustomThresholdRatioDataSql = "update `t_s_monitor_goods_ratio_value` set channelId=?,vip=?,thresholdType=?,thresholdRange=?,thresholdRatio=?,del=? where dbId=? ";
    @Override
    protected boolean updateData(CustomThresholdRatioData data) {
        data.setBeginWriteTime();
        int[] types = new int[]{Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER, Types.BIGINT};
        Object[] values = new Object[7];
        int i = 0;
        values[i++] = data.getChannelId();
        values[i++] = data.getVip();
        values[i++] = data.getThresholdType();
        values[i++] = JSON.toJSONString(data.getThresholdRange());
        values[i++] = data.getThresholdRatio();
        values[i++] = data.getDel();
        values[i++] = data.getDbId();
        if (JdbcTemplateHelper.getJdbcTemplate().update(updateCustomThresholdRatioDataSql, values, types) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCustomThresholdRatioDataSql = "insert into `t_s_monitor_goods_ratio_value`(dbId,channelId,vip,thresholdType,thresholdRange,thresholdRatio,del) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CustomThresholdRatioData data) {
        data.setBeginWriteTime();
        int[] types = new int[]{Types.BIGINT, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER};
        Object[] values = new Object[7];
        int i = 0;
        values[i++] = data.getDbId();
        values[i++] = data.getChannelId();
        values[i++] = data.getVip();
        values[i++] = data.getThresholdType();
        values[i++] = JSON.toJSONString(data.getThresholdRange());
        values[i++] = data.getThresholdRatio();
        values[i++] = data.getDel();
        if (JdbcTemplateHelper.getJdbcTemplate().update(insertCustomThresholdRatioDataSql, values, types) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CustomThresholdRatioData parseData(SqlRowSet rs) throws SQLException {
        CustomThresholdRatioData customThresholdRatioData = new CustomThresholdRatioData();
        customThresholdRatioData.setDbId(rs.getLong("dbId"));
        customThresholdRatioData.setChannelId(rs.getInt("channelId"));
        customThresholdRatioData.setVip(rs.getInt("vip"));
        customThresholdRatioData.setThresholdType(rs.getInt("thresholdType"));
        customThresholdRatioData.setThresholdRange(JSON.parseObject(rs.getString("thresholdRange"), new TypeReference<List<Integer>>() {}));
        customThresholdRatioData.setThresholdRatio(rs.getInt("thresholdRatio"));
        customThresholdRatioData.setDel(rs.getInt("del"));

        return customThresholdRatioData;
    }
}
